package com.droid.lib.controls.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.avast.android.dialogs.fragment.DatePickerDialogFragment;
import com.avast.android.dialogs.fragment.ListDialogFragment;
import com.avast.android.dialogs.fragment.ProgressDialogFragment;
import com.avast.android.dialogs.fragment.SimpleDialogFragment;
import com.avast.android.dialogs.fragment.TimePickerDialogFragment;
import com.avast.android.dialogs.iface.IDateDialogListener;
import com.avast.android.dialogs.iface.ISimpleDialogCancelListener;
import com.avast.android.dialogs.iface.ISimpleDialogListener;
import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.demo.adapter.MenuAdapter;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2016/9/21.
 */
public class DialogsFragment extends Fragment implements ISimpleDialogListener,
        IDateDialogListener,
        ISimpleDialogCancelListener {
    private Context mContext;
    private MenuAdapter mAdapter;
    private static final int REQUEST_PROGRESS = 1;
    private static final int REQUEST_LIST_SIMPLE = 9;
    private static final int REQUEST_LIST_MULTIPLE = 10;
    private static final int REQUEST_LIST_SINGLE = 11;
    private static final int REQUEST_DATE_PICKER = 12;
    private static final int REQUEST_TIME_PICKER = 13;
    private static final int REQUEST_SIMPLE_DIALOG = 42;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_dialogs, null);
        ListView listview = (ListView) view.findViewById(R.id.listview);
        mAdapter = new MenuAdapter(mContext);
        listview.setAdapter(mAdapter);
        ArrayList<String> arrString = new ArrayList<String>();
        arrString.add("Simple dialog: only message");
        arrString.add("Simple dialog: long message and title");
        arrString.add("Simple dialog: 3 buttons &amp; callbacks");
        arrString.add("Simple dialog: stacked buttons");
        arrString.add("List dialog");
        arrString.add("Single choice list dialog");
        arrString.add("Multiple choice list dialog");
        arrString.add("Progress dialog");
        arrString.add("Time picker dialog");
        arrString.add("Date picker dialog");
        arrString.add("Custom dialog");
        mAdapter.setList(arrString);
        mAdapter.setOnClicker(new MenuAdapter.OnClicker() {
            @Override
            public void onclick(int position) {
                switch (position) {
                    case 0:
                        SimpleDialogFragment.createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setMessage("Love. Can know all the math in the \'verse but take a boat in the air that you don\'t " +
                                        "love? She\'ll shake you off just as sure as a turn in the worlds. Love keeps her in the air when " +
                                        "she oughtta fall down...tell you she\'s hurtin\' \'fore she keens...makes her a home.").show();

                        break;
                    case 1:
                        SimpleDialogFragment.createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setTitle("More Firefly quotes:").setMessage
                                ("Wash: \"Psychic, though? That sounds like something out of science fiction.\"\n\nZoe: \"We live" +
                                        " " +
                                        "in a space ship, dear.\"\nWash: \"Here lies my beloved Zoe, " +
                                        ("my autumn flower ... somewhat less attractive now that she's all corpsified and gross" +
                                                ".\"\n\nRiver Tam: \"Also? I can kill you with my brain.\"\n\nKayle: \"Going on a year now, nothins twixed my neathers not run on batteries.\" \n" +
                                                "Mal: \"I can't know that.\" \n" +
                                                "Jayne: \"I can stand to hear a little more.\"\n\nWash: \"I've been under fire before. " +
                                                "Well ... I've been in a fire. Actually, I was fired. I can handle myself.\""))
                                .setNegativeButtonText("Close")
                                .show();
                        break;
                    case 2:
                        SimpleDialogFragment.createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setTitle("Do you like this quote?")
                                .setMessage("Jayne: \"Shiny. Let's be bad guys.\"")
                                .setPositiveButtonText("Love")
                                .setNegativeButtonText("Hate")
                                .setNeutralButtonText("WTF?")
                                .setTargetFragment(DialogsFragment.this,REQUEST_SIMPLE_DIALOG)
                                .show();
                        break;
                    case 3:
                        SimpleDialogFragment.createBuilder(mContext, getActivity().getSupportFragmentManager()).setMessage("How will you decide?")
                                .setPositiveButtonText("Time for some thrillin' heroics!").setNegativeButtonText("Misbehave")
                                .setNeutralButtonText("Keep flying").show();
                        break;
                    case 4:
                        ProgressDialogFragment.createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setMessage("Mal: I\'m just waiting to see if I pass out. Long story.")
                                .setRequestCode(REQUEST_PROGRESS)
                                .show();
                        break;
                    case 5:
                        ListDialogFragment
                                .createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setTitle("Your favorite character:")
                                .setItems(new String[]{"Jayne", "Malcolm", "Kaylee",
                                        "Wash", "Zoe", "River"})
                                .setRequestCode(REQUEST_LIST_SIMPLE)
                                .show();
                        break;
                    case 6:
                        ListDialogFragment
                                .createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setTitle("Your favorite character:")
                                .setItems(new String[]{"Jayne", "Malcolm", "Kaylee",
                                        "Wash", "Zoe", "River"})
                                .setRequestCode(REQUEST_LIST_SINGLE)
                                .setChoiceMode(AbsListView.CHOICE_MODE_SINGLE)
                                .show();
                        break;
                    case 7:
                        ListDialogFragment
                                .createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setTitle("Your favorite character:")
                                .setItems(new String[]{"Jayne", "Malcolm", "Kaylee",
                                        "Wash", "Zoe", "River"})
                                .setRequestCode(REQUEST_LIST_MULTIPLE)
                                .setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE)
                                .setCheckedItems(new int[]{1, 3})
                                .show();

                        break;

                    case 8:
                        TimePickerDialogFragment
                                .createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setDate(new Date())
                                .setPositiveButtonText(android.R.string.ok)
                                .setNegativeButtonText(android.R.string.cancel)
                                .setRequestCode(REQUEST_TIME_PICKER)
                                .show();
                        break;
                    case 9:
                        DatePickerDialogFragment
                                .createBuilder(mContext, getActivity().getSupportFragmentManager())
                                .setDate(new Date())
                                .setPositiveButtonText(android.R.string.ok)
                                .setNegativeButtonText(android.R.string.cancel)
                                .setTargetFragment(DialogsFragment.this,REQUEST_DATE_PICKER)
                                .show();
                        break;
                    case 10:
                        JayneHatDialogFragment.show(getActivity());
                        break;

                }
            }
        });
        return view;

    }
    // ISimpleDialogCancelListener

    @Override
    public void onCancelled(int requestCode) {
        switch (requestCode) {
            case REQUEST_SIMPLE_DIALOG:
                Toast.makeText(mContext, "Dialog cancelled", Toast.LENGTH_SHORT).show();
                break;
            case REQUEST_PROGRESS:
                Toast.makeText(mContext, "Progress dialog cancelled", Toast.LENGTH_SHORT).show();
                break;
            case REQUEST_LIST_SIMPLE:
            case REQUEST_LIST_SINGLE:
            case REQUEST_LIST_MULTIPLE:
                Toast.makeText(mContext, "Nothing selected", Toast.LENGTH_SHORT).show();
                break;
            case REQUEST_DATE_PICKER:
                Toast.makeText(mContext, "Date picker cancelled", Toast.LENGTH_SHORT).show();
                break;
            case REQUEST_TIME_PICKER:
                Toast.makeText(mContext, "Time picker cancelled", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    // ISimpleDialogListener

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            Toast.makeText(mContext, "Positive button clicked", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            Toast.makeText(mContext, "Negative button clicked", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            Toast.makeText(mContext, "Neutral button clicked", Toast.LENGTH_SHORT).show();
        }
    }

    // IDateDialogListener

    @Override
    public void onNegativeButtonClicked(int resultCode, Date date) {
        String text = "";
        if (resultCode == REQUEST_DATE_PICKER) {
            text = "Date ";
        } else if (resultCode == REQUEST_TIME_PICKER) {
            text = "Time ";
        }

        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT);
        Toast.makeText(mContext, text + "Cancelled " + dateFormat.format(date), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPositiveButtonClicked(int resultCode, Date date) {
        String text = "";
        if (resultCode == REQUEST_DATE_PICKER) {
            text = "Date ";
        } else if (resultCode == REQUEST_TIME_PICKER) {
            text = "Time ";
        }

        DateFormat dateFormat = DateFormat.getDateTimeInstance();
        Toast.makeText(mContext, text + "Success! " + dateFormat.format(date), Toast.LENGTH_SHORT).show();
    }


    private boolean isDarkTheme() {
        boolean darkTheme = false;
        //Try-catch block is used to overcome resource not found exception
        try {
            TypedValue val = new TypedValue();

            //Reading attr value from current theme
            mContext.getTheme().resolveAttribute(com.avast.android.dialogs.R.attr.isLightTheme, val, true);

            //Passing the resource ID to TypedArray to get the attribute value
            TypedArray arr = mContext.obtainStyledAttributes(val.data, new int[]{com.avast.android.dialogs.R.attr.isLightTheme});
            darkTheme = !arr.getBoolean(0, false);
            arr.recycle();
        } catch (RuntimeException e) {
            //Resource not found , so sticking to light theme
            darkTheme = false;
        }
        return darkTheme;
    }

}
